<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />

    <style type="text/css">
        .class-title {
            margin-top: 10px;
            margin-left: 10px;
            margin-right: 10px;
            font-size: 20px;
            font-weight: bold;
            color: #00BFFF;
        }

        .class-hr {
            margin-top: 5px;
            height: 1px;
            border: none;
            border-top: 1px solid #DCDCDC;
        }

        .class-seqno {
            margin-top: 3px;
            margin-left: 10px;
            margin-right: 25px;
            font-size: 10px;
            color: #808080;
            font-weight: bold;
            text-align: right;
        }

        .class-table {
            width: 100%;
            text-align: left;
            color: black;
            font-size: 13px;
            margin-left: 2%;
            width: 100%;
            table-layout: fixed;
        }

        .class-td {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            首件检测单
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:80px;">
        <div class="aui-btn aui-btn-info aui-btn-block" onclick='reCreateChkBillDetail()'>重新检测</div>
        <div class="aui-btn aui-btn-info aui-btn-block" style="margin-top:20px;" onclick='doSearchChkBillDetails()'>
            查询检测单</div>
    </div>

    <div id="divhtml" style="display: block;width:100%;overflow-y: scroll;margin-top:10px;"></div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";   //设备唯一号
    var workCenterCode = "";  //生产线编码
    var workCellCode = "";    //工位编码
    var opCode = "";          //工序编码
    var shiftCode = "";       //班次编码
    var shiftName = "";       //班次名称
    var proddate = "";        //生产日期
    var empName = "";         //上岗人员 信息
    var list;                 //检测单结果集

    var chkTypeCode = ""; //检测单类型
    apiready = function () {
        var query = api.pageParam.query;
        chkTypeCode = query.chkTypeCode;

        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');

        //查询当前上岗的产线、日期、班次、工序
        searchWorkCenterData();
    };

    //查询产线工位信息
    function searchWorkCenterData() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getOpCodeByUniCode";
        var data = {
            values: {
                clientCode: equipUniqueId,
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    //标题
                    workCenterCode = myobj.workCenterCode;  //生产线编码
                    workCellCode = myobj.workCellCode;      //工位编码
                    opCode = myobj.opCode;                  //工序编码

                    if (opCode != null && opCode != "") {

                        //1:查询当前操作人员信息
                        searchDutyPersonData();
                    } else {
                        api.toast({
                            msg: "唯一号【" + equipUniqueId + "】未配置工序信息",
                            duration: 3000
                        });
                    }
                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询人员上岗信息
    function searchDutyPersonData() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getProductionAssemblyTake";
        var data = {
            values: {
                workCenterCode: workCenterCode,
                opCode: opCode,
                workCellCode: workCellCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {
                    empName = myobj.empName;      //上岗人员
                    shiftCode = myobj.shiftCode;  //班次编码
                    shiftName = myobj.shiftName;  //班次名称
                    proddate = myobj.proddate;    //上岗日期

                    //查询最新的检测单（首件）
                    doSearchChkBillDetails();
                } else {
                    //清空人员信息
                    clear();
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //清空人员上岗信息
    function clear() {
        empName = "";
        shiftCode = "";
        shiftName = "";
        proddate = "";
    }

    //查询最新检测单明细项
    function doSearchChkBillDetails() {
        if (opCode == null || opCode == "" || opCode == undefined) {
            api.toast({
                msg: "唯一号【" + equipUniqueId + "】未配置工序信息,不允许操作",
                duration: 3000
            });
            return;
        }
        if (empName == null || empName == "" || empName == undefined) {
            api.toast({
                msg: "未上岗,不允许操作",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getChkBillDetailsSjjc";
        var data = {
            values: {
                chkTypeCode: chkTypeCode,
                proddate: proddate,
                workCenterCode: workCenterCode,
                workCellCode: workCellCode,
                shiftCode: shiftCode,
                opCode: opCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {
                    list = eval(myobj.list);
                    if (list != null && list.length > 0) {
                        createTable();
                    } else {
                        $api.html($api.byId("divhtml"), "");
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $api.html($api.byId("divhtml"), "");
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //构造表格数据
    function createTable() {
        var html = "";
        //遍历
        for (var i = 0; i < list.length; i++) {
            // 序号
            html += "<div class='class-seqno'>";
            html += "<span>" + (i + 1) + "</span>";
            html += "</div>";
            //表格
            html += "<div style='margin-left:10px;margin-right:15px;'>";
            html += "<table class='class-table'>";
            html += "<tr>";
            html += "<td class='class-td'>检测单号:    " + list[i].chkBillCode + "</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td class='class-td'>物料编码:    " + list[i].mrlCode + "</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td class='class-td'>物料名称:    " + list[i].mrlName + "</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td class='class-td'>序  列  号:    " + list[i].serialNo + "</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td class='class-td'>工序编码:    " + list[i].opCode + "</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td class='class-td'>工序名称:    " + list[i].opName + "</td>";
            html += "</tr>";
            html += "<tr>";
            html += "<td class='class-td'>工  单  号:    " + list[i].workOrderCode + "</td>";
            html += "</tr>";
            html += "<tr>";
            if (list[i].chkResult == "0" || list[i].chkResult == "1") {
                html += "<td class='class-td' style='color:red;'>处理结果:    " + list[i].chkResultName + "</td>";
            } else {
                html += "<td class='class-td' style='color:green;'>处理结果:    " + list[i].chkResultName + "</td>";
            }
            html += "</tr>";
            if (list[i].chkResult == "0") {
                html += "<tr>";
                html += "<td class='class-td'>";
                html += "<div class='aui-btn aui-btn-info' onclick='deal(\"" + list[i].chkBillDetailsId + "\",\"" + list[i].chkBillCode + "\")'>";
                html += "处理</div></td>";
                html += "</tr>";
            }
            html += "</table>";
            html += "<hr class='class-hr' />";
            html += "</div>";
        }

        $api.html($api.byId("divhtml"), html);
    }

    //处理
    function deal(chkBillDetailsId, chkBillCode) {
        api.openWin({
            name: 'qcmChkBillDealSJJC',
            url: 'qcmChkBillDealSJJC.html',
            pageParam: {
                chkBillDetailsId: chkBillDetailsId,
                chkBillCode: chkBillCode,
                name: "qcmChkBillSJJC",
            }
        });
    }

    //重新检测
    function reCreateChkBillDetail() {
        if (list == null || list.length == 0) {
            api.toast({
                msg: "当前工序没有首件检测单，无法重新检测!",
                duration: 3000
            });
            return;
        }

        if (list[0].chkResult == "0") {
            api.toast({
                msg: "当前首件检测单未判定，请处理!",
                duration: 3000
            });
            return;
        }

        if (list[0].chkResult == "5" || list[0].chkResult == "7") {
            api.toast({
                msg: "当前首件检测单已合格，无需重新检测!",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/createChkBillDetailsSjjc";
        var data = {
            values: {
                chkBillDetailsId: list[0].chkBillDetailsId,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    doSearchChkBillDetails();

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    doSearchChkBillDetails();
                }
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>